package shopping

import (
	"dhfshop/models/shopping"
	"time"
	"dhfshop/controllers/base"
	"github.com/astaxie/beego/validation"
)

type ActivityPageController struct {
	base.BaseController
}

func (this *ActivityPageController) ActivitysList() {
	query,_,fields,order_by,offset,limit,err := this.GetQueryPara()
	if err != nil {
	   this.ServeError(err.Error())
	   return
	}
	l, cnt, err := shopping.GetAllActivityPages(query,fields,order_by,offset,limit)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		TotalCount  int64               `json:"total_count"`
		Activitys   []*shopping.ActivityPage  `json:"activity_pages"`
	}{TotalCount: cnt, Activitys: l}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *ActivityPageController) Post() {
	var v shopping.ActivityPage
	this.ParseForm(&v)
	valid := validation.Validation{}
	//valid.Required(v.Name, "name")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return
	}
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	_, err := shopping.AddActivityPage(&v)
	if err != nil {
	   this.ServeError(err.Error())
		return 
	}
	rsp := struct {
		ActivityPageId int64        `json:"activity_page_id"`
	    Created    time.Time    `json:"created"`
	}{ActivityPageId: v.ActivityPageId, Created:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *ActivityPageController) Put() {
	v := shopping.ActivityPage{}
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.ActivityPageId, "activity_page_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	excludeArr := []string{""}
	updateFields, err := this.GetUpdateFields(v, "", "form", excludeArr)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	if err := shopping.UpdateActivityPageById(&v, updateFields...); err != nil{
		this.ServeError(err.Error())
		return
	}
	
	r := struct {
	    ActivityPageId int64   `json:"activity_page_id"`
	    Modified   time.Time   `json:"modified"`
	}{ActivityPageId:v.ActivityPageId, Modified:time.Now().UTC()}
	this.Data["json"] = r
	this.ServeJSON()
}

func (this *ActivityPageController) Delete() {
    partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
    //curUserId := this.Ctx.Input.GetData("curUserId").(int)
	v := struct {
		PartnerId   int64    `form:"-"  valid:"Required"`
	    ActivityPageId  int64  `form:"activity_page_id" valid:"Required"`
		Id          int64  `form:"id"`
	}{PartnerId: partnerId} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.ActivityPageId, "activity_page_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return
	}
	if err := shopping.DeleteActivityPage(v.ActivityPageId); err != nil {
		this.ServeError(err.Error())
		return
	}
	r := struct {
	    ActivityPageId int64       `json:"activity_page_id"`
	    Modified   time.Time   `json:"modified"`
	}{ActivityPageId:v.ActivityPageId, Modified:time.Now().UTC()}
	this.Data["json"] = r
	this.ServeJSON() 
}

